package com.cn.yang.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.news.NewsModel;
import com.cn.yang.service.NewsService;
import com.cn.yang.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/news/addNews")
public class AddNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer classify = Integer.valueOf(req.getParameter("newsClassify"));
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String time = req.getParameter("newsTime");
        
        String contentall = req.getParameter("contentall");
        String author = req.getParameter("author");
        NewsModel model = new NewsModel();
        model.setTitle(title);
        model.setContent(content);
        model.setTime(time);
        model.setType(classify);
        model.setContentall(contentall);
        model.setAuthor(author);

        NewsService service = new NewsServiceImpl();
        int result = service.addNews(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
